package cn.itcast.openai;

import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.chat.completions.ChatCompletionCreateParams;

/**
 * @author wys
 * @version V1.0
 * @date 2025-04-19 16:44
 */
public class CompletionsDemo {
    public static void main(String[] args) {
        OpenAIClient client = OpenAIOkHttpClient.builder()
                // .baseUrl("https://api.chatanywhere.tech/v1")
                // .apiKey(System.getenv("OPENAI_API_KEY"))
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                .build();

        ChatCompletionCreateParams createParams = ChatCompletionCreateParams.builder()
                .model("qwen-plus")
                .addSystemMessage("you are trump")
                .addUserMessage("china NO.1")
                .build();

        client.chat().completions()
                .create(createParams)
                .choices()
                .stream()
                .flatMap(choice -> choice.message().content().stream())
                .forEach(System.out::println);

    }
}
